﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.Object
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009-11-03 16:35:41
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Object
{
    /// <summary>
    ///  Base dictionary for extension object
    /// </summary>
    /// <typeparam name="T1">key</typeparam>
    /// <typeparam name="T2">value</typeparam>
    /// <typeparam name="T3">value inner type</typeparam>
    public class ExtensionDictionary<T1, T2, T3> : IExtensionDictionary<T1, T2>
        where T2:IExtensionObject<T3>
    {
        private IDictionary<T1, T2> m_dictionary = new Dictionary<T1, T2>();
        private T3 m_extension = default(T3);

        public ExtensionDictionary()
        {
            
        }
        public ExtensionDictionary(T3 t)
        {
            this.m_extension = t;
        }
        /// <summary>
        /// Get or Set the additional object used when object is added into m_dictionary
        /// </summary>
        public T3 AdditionalData
        {
            get { return this.m_extension; }
            set { this.m_extension = value; }
        }

        #region IDictionary<T1,T2> 成员

        public void Add(T1 key, T2 value)
        {
            if (!m_dictionary.Keys.Contains(key) && value.Load(m_extension))
            {
                this.m_dictionary.Add(key, value);
            }
        }

        public bool ContainsKey(T1 key)
        {
            return this.m_dictionary.ContainsKey(key);
        }

        public ICollection<T1> Keys
        {
            get { return this.m_dictionary.Keys; }
        }

        public bool Remove(T1 key)
        {
            return this.m_dictionary.Remove(key);
        }

        public bool TryGetValue(T1 key, out T2 value)
        {
            return this.m_dictionary.TryGetValue(key, out value);
        }

        public ICollection<T2> Values
        {
            get { return this.m_dictionary.Values; }
        }

        public T2 this[T1 key]
        {
            get
            {
               return this.m_dictionary[key];
            }
            set
            {
                this.m_dictionary[key] = value;
            }
        }

        #endregion

        #region ICollection<KeyValuePair<T1,T2>> 成员

        public void Add(KeyValuePair<T1, T2> item)
        {
            this.m_dictionary.Add(item);
        }

        public void Clear()
        {
            this.m_dictionary.Clear();
        }

        public bool Contains(KeyValuePair<T1, T2> item)
        {
            return this.m_dictionary.Contains(item);
        }

        public void CopyTo(KeyValuePair<T1, T2>[] array, int arrayIndex)
        {
            this.m_dictionary.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this.m_dictionary.Count; }
        }

        public bool IsReadOnly
        {
            get { return this.m_dictionary.IsReadOnly; }
        }

        public bool Remove(KeyValuePair<T1, T2> item)
        {
            return this.m_dictionary.Remove(item);
        }

        #endregion

        #region IEnumerable<KeyValuePair<T1,T2>> 成员

        public IEnumerator<KeyValuePair<T1, T2>> GetEnumerator()
        {
            return this.m_dictionary.GetEnumerator();
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}
